package com.china08.yxyapi.api.home.weikecheng;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter.ResSubjectPhase;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.mg.YxyUserCenterRepository;

/**
 * 微课首页初始化---初始化默认 :小学+数学+区域微课+最新上传倒序
 * 
 */
@CrossOrigin
@RestController
public class WeikeHomeContentController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @Autowired
    private YxyUserCenterRepository yxyUserCenterRepository;

    @Autowired
    private YxyResSubjectRepository subjectRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/weike/content/init", method = RequestMethod.GET)
    public RespModel process(HttpServletRequest request, HttpServletResponse response) {

        // ##
        String userId = getProfile(request, response).getId();
        RespModel model = new RespModel();
        if (StringUtils.isNotBlank(userId)) {
            YxyUserCenter center = yxyUserCenterRepository.findByUserId(userId);
            if (center != null) {
                ResSubjectPhase lastWeike = center.getLastWeikeContentView();
                if (lastWeike != null) {
                    if (NumberUtils.isNumber(lastWeike.getSubjectId())) {
                        YxyResSubject yxyResSubject = subjectRepository.findByName(lastWeike.getSubjectName());
                        model.subject = yxyResSubject != null ? yxyResSubject.getId() : lastWeike.getSubjectId();
                    } else {
                        model.subject = lastWeike.getSubjectId();
                    }
                    model.phase = lastWeike.getPhaseId();
                    model.phaseName = lastWeike.getPhaseName();
                    model.subjectName = lastWeike.getSubjectName();
                    model.semester = lastWeike.getSemester();
                    model.semesterName = lastWeike.getSemesterName();
                    model.version = lastWeike.getVersionId();
                    model.versionName = lastWeike.getVersionName();
                    model.bookId = lastWeike.getBookId();
                }
            }
        }
        List<YxyResBookContents> listcoContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(model.bookId, 1);
        BookContentRespModel respModel = null;
        for (YxyResBookContents yxyResBookContents : listcoContents) {
            respModel = new BookContentRespModel();
            respModel.setId(yxyResBookContents.getId());
            respModel.setName(yxyResBookContents.getName());
            respModel.setpId(yxyResBookContents.getpId());
            model.bookContent.add(respModel);
        }

        model.setMessage("成功");
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String phaseName = "小学";

        public String subjectName = "数学";

        public String versionName = "人教课标版";

        public String phase = "01";

        public String subject = "jcsub02";

        public String version = "bb08";

        public String semester = "V008";

        public String semesterName = "一年级上册";

        public String bookId = "21354";

        public List<BookContentRespModel> bookContent = new ArrayList<BookContentRespModel>();
    }

    protected static class BookContentRespModel {
        public String id;
        public String pId;
        public String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

}
